//
//  CorgiHomeViewModel.swift
//  weibo
//
//  Created by Corgi on 17/3/24.
//  Copyright © 2017年 cocoaHangTou. All rights reserved.
//

import UIKit
import SDWebImage

class CorgiHomeViewModel: NSObject {

    var dataArray: [CorgiStatusViewModel] = [CorgiStatusViewModel]()
    
}


extension CorgiHomeViewModel {

    func getHomeData(isPullUP: Bool, finish: @escaping (Bool, Int) -> ()) {
        
        var sinceID: Int64 = 0
        
        var maxID: Int64 = 0
        
        if isPullUP == true {

            maxID = dataArray.last?.homeModel?.id ?? 0
            
            if maxID > 0 {
        
                maxID -= 1
    
            }
            
        } else {
        
            sinceID = dataArray.first?.homeModel?.id ?? 0
        
        }
        
        HMNetworkTools.shared.homeLoadData (since_id: sinceID, max_id: maxID){ (result, error) in
            if error != nil {
                
                finish(false, 0)
                return
            }
            
            guard let res = result as? [String : Any] else {
                
                finish(false, 0)
                return
            }
            
            guard let resArr = res["statuses"] as? [[String : Any]] else {
                
                finish(false, 0)
                return
            }
            
            let statusArr = NSArray.yy_modelArray(with: CorgiHomeModel.self, json: resArr) as! [CorgiHomeModel]
            
            //            print(statusesArr)
            
            var tempArr: [CorgiStatusViewModel] = [CorgiStatusViewModel]()
            
            for homeModel in statusArr {
            
                let statusViewModel = CorgiStatusViewModel()
                
                statusViewModel.homeModel = homeModel
                
                tempArr.append(statusViewModel)
            
            }
            
            self.downLoadSingleImage(tempArray: tempArr, finish: finish)
            
            if isPullUP {
            
                self.dataArray = self.dataArray + tempArr
            
            } else {
            
                self.dataArray = tempArr + self.dataArray
                
            }
        }
    }
    
    private func downLoadSingleImage(tempArray: [CorgiStatusViewModel],finish: @escaping (Bool, Int) -> ()) {
        
        let group = DispatchGroup()
        
        for statusViewModel in tempArray {
        
            if statusViewModel.homeModel?.pic_urls?.count == 1 {
                
                group.enter()
                
                SDWebImageManager.shared().downloadImage(with: URL(string: statusViewModel.homeModel?.pic_urls?.first?.thumbnail_pic ?? ""), options: [], progress: nil, completed: { (image, error, _, _, _) in
                  
                    group.leave()
                    
                })
            }
            
            if statusViewModel.homeModel?.retweeted_status?.pic_urls?.count == 1 {
            
                group.enter()
                
                SDWebImageManager.shared().downloadImage(with: URL(string: statusViewModel.homeModel?.retweeted_status?.pic_urls?.first?.thumbnail_pic ?? ""), options: [], progress: nil, completed: { (image, error, _, _, _) in
                    
                    group.leave()
                    
                })
            }
        }
        
        group.notify(queue: DispatchQueue.main) {
            
            //全部完成
            finish(true, tempArray.count)
        }
    }
}
